package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsCoupon;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.AdminCouponService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;


@RequestMapping("/admin/coupon")
@CrossOrigin
@RestController
public class AdminCouponController {

    @Autowired
    private AdminCouponService adminCouponService;

    /**
     * 优惠券列表
     * @param page
     * @param limit
     * @param name
     * @param type
     * @param status
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:coupon:list")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button = "优惠券列表")
    public Object couponList(@RequestParam(defaultValue = "1")Integer page,
                             @RequestParam(defaultValue = "20")Integer limit,
                             String name,Integer type,Integer status,
                             String sort,String order){

        IPage<DtsCoupon> iPage = adminCouponService.selectCouponList(page,limit,name,type,status,sort,order);

        long total = iPage.getTotal();
        List<DtsCoupon> records = iPage.getRecords();

        DtsCoupon[] items = new DtsCoupon[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }


    //优惠券的详细信息
    @GetMapping("/read")
    @RequiresPermissions("admin:coupon:read")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券详情"},button = "优惠券基本信息")
    public Object couponRead(Integer id){
        DtsCoupon dtsCoupon = adminCouponService.selectCouponById(id);
        return ResponseUtil.ok(dtsCoupon);
    }

    /**
     * 使用此优惠券的用户列表
     * @param page
     * @param limit
     * @param couponId
     * @param userId
     * @param status
     * @param sort
     * @param order
     * @return
     */
    @GetMapping("/listuser")
    @RequiresPermissions("admin:coupon:listuser")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理","优惠券详情"},button = "使用此优惠券的用户列表")
    public Object couponListuser(@RequestParam(defaultValue = "1")Integer page,
                                 @RequestParam(defaultValue = "20") Integer limit,
                                 Integer couponId,Integer userId,Integer status,
                                 String sort,String order){
        IPage<DtsCouponUser> iPage = adminCouponService.selectCouponUserList(page,limit,couponId,userId,status,sort,order);

        long total = iPage.getTotal();
        List<DtsCouponUser> records = iPage.getRecords();

        DtsCouponUser[] items = new DtsCouponUser[records.size()];

        for (int i = 0; i < records.size(); i++) {
            items[i] = records.get(i);
        }

        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", items);

        return ResponseUtil.ok(data);
    }

    //优惠券添加
    @PostMapping("/create")
    @RequiresPermissions("admin:coupon:create")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button = "优惠券添加")
    public Object couponCreate(@RequestBody DtsCoupon dtsCoupon){

        DtsCoupon data = adminCouponService.insertCoupon(dtsCoupon);

        return ResponseUtil.ok(data);
    }

    //优惠券修改
    @PostMapping("/update")
    @RequiresPermissions("admin:coupon:update")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button = "优惠券修改")
    public Object couponUpdate(@RequestBody DtsCoupon dtsCoupon){
        DtsCoupon data = adminCouponService.updateCoupon(dtsCoupon);
        return ResponseUtil.ok(data);
    }

    //优惠券删除
    @PostMapping("/delete")
    @RequiresPermissions("admin:coupon:delete")
    @RequiresPermissionsDesc(menu = {"推广管理","优惠券管理"},button = "优惠券删除")
    public Object couponDelete(@RequestBody DtsCoupon dtsCoupon){
        adminCouponService.deleteCoupon(dtsCoupon.getId());
        return ResponseUtil.ok();
    }

}
